/******************************************************************************
 * Author:      671643387
 * Created:     2016/9/29
 *****************************************************************************/

/**
 * 生成麻将牌
 * @param category
 * @returns {Array}
 */
function genMahjong(category, roomType, selectIsWind) {
    var mahjongs = [];
    var categoryConf = null;

    // 带风
    if(1 == roomType || 2 == roomType && selectIsWind) {
        categoryConf = gConfMahjongWind[category];
    }else if(2 == roomType){     // 不带风
        categoryConf = gConfMahjong[category];
    }

    for (var type in categoryConf) {
        if (!categoryConf.hasOwnProperty(type)) {
            continue;
        }
        type = +type;
        var typeConf = categoryConf[type];
        for (var val in typeConf) {
            if (!typeConf.hasOwnProperty(val)) {
                continue;
            }

            val = +val;
            for (var j = 1; j <= 4; ++j) {
                mahjongs.push({
                    type: type,
                    value: val
                });
            }
        }
    }

    var randomMahjongs = [];
    while (mahjongs.length > 0) {
        var idx = Math.floor(Math.random() * mahjongs.length);
        randomMahjongs.push(mahjongs[idx]);
        mahjongs.splice(idx, 1);
    }

    return randomMahjongs;
}

/**
 * 排序手牌
 * @param mahjongs
 * @returns {Array}
 */
function sortCards(mahjongs) {
    var sortedArrs = {};
    var newMahjongs = [];
    var types = {};
    // 统计类型
    mahjongs.forEach(function(mahjong){
        types[mahjong.type] = 1;
    });

    for (var type in types) {
        if (!types.hasOwnProperty(type)) {
            continue;
        }

        type = +type;
        var values = [];
        mahjongs.forEach(function (mahjong) {
            if (mahjong.type == type) {
                values.push(+mahjong.value);
            }
        });
        values.sort();

        values.forEach(function (val) {
            newMahjongs.push({
                type: type,
                value: val,
            });
        });
    }
    return newMahjongs;
}


/**
 * Test牌
 * @param category
 * @returns {Array}
 */
function tesetMahjong(category, roomType, selectIsWind) {
    var mahjongs = [];
    var categoryConf = null;

    // 带风
    if(1 == roomType || 2 == roomType && selectIsWind) {
        categoryConf = gConfMahjongWind[category];
    }else if(2 == roomType){     // 不带风
        categoryConf = gConfMahjong[category];
    }

    for (var type in categoryConf) {
        if (!categoryConf.hasOwnProperty(type)) {
            continue;
        }
        type = +type;
/////////////////////////////////////////
        // 测试数据
        if(type == 1)
        {
            continue;
        }
/////////////////////////////////////////

        var typeConf = categoryConf[type];
        for (var val in typeConf) {
            if (!typeConf.hasOwnProperty(val)) {
                continue;
            }

            val = +val;
/////////////////////////////////////////
            // 测试数据
            if(type < 3 && val < 5){
                continue;
            }
/////////////////////////////////////////
            for (var j = 1; j <= 4; ++j) {
                mahjongs.push({
                    type: type,
                    value: val
                });
            }
        }
    }

    var randomMahjongs = [];
    var pos = 0;
    while (mahjongs.length > pos) {
        //var idx = Math.floor(Math.random() * mahjongs.length);
        randomMahjongs.push(mahjongs[pos]);
        //mahjongs.splice(idx, 1);
        ++pos;
    }

    return randomMahjongs;
}
exports.genMahjong = genMahjong;
exports.sortCards = sortCards;
exports.tesetMahjong = tesetMahjong;